package com.yx.gsyd.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yx.gsyd.entity.SysManager;
import com.yx.gsyd.entity.TaskList;
import com.yx.gsyd.entity.json.HgfsJson;
import com.yx.gsyd.entity.model.TasklistModel;
import com.yx.gsyd.entity.search.TaskListSearch;
import com.yx.gsyd.entity.view.TaskListView;
import com.yx.gsyd.mapper.TaskListMapper;
import com.yx.gsyd.qpage.QPage;
import com.yx.gsyd.service.ISmsService;
import com.yx.gsyd.service.ISysManagerService;
import com.yx.gsyd.service.ITaskListService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yx.gsyd.sms.AliSMS;
import com.yx.gsyd.util.IdentityUtil;
import com.yx.gsyd.util.ResultView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-06-29
 */
@Service
public class TaskListServiceImpl extends ServiceImpl<TaskListMapper, TaskList> implements ITaskListService {

    @Autowired
    private ISysManagerService iSysManagerService;

    @Autowired
    private ISmsService iSmsService;

    @Transactional
    @Override
    public ResultView createTask(TaskList taskList) {
        taskList.setAddtime(new Date());
        taskList.setStatus(1);
        //计算流水金额之和
        BigDecimal bigDecimal=BigDecimal.ZERO;
        //信用贷款额度
        BigDecimal xyMoney=taskList.getXydje();
        List<HgfsJson> hgfsJsonList= JSON.parseArray(taskList.getHkfsjson(),HgfsJson.class);
        for (HgfsJson hgfsJson : hgfsJsonList) {
            if(hgfsJson.getName().equals("微信回款")){
                taskList.setWxhk(hgfsJson.getValue());
                bigDecimal=bigDecimal.add(new BigDecimal(hgfsJson.getValue()));
            }else if (hgfsJson.getName().equals("支付宝回款")) {
                taskList.setZfbhk(hgfsJson.getValue());
                bigDecimal=bigDecimal.add(new BigDecimal(hgfsJson.getValue()));
            }else if (hgfsJson.getName().equals("银行卡回款")) {
                taskList.setYhkhk(hgfsJson.getValue());
                bigDecimal=bigDecimal.add(new BigDecimal(hgfsJson.getValue()));
            }else if (hgfsJson.getName().equals("其他回款")) {
                taskList.setQthk(hgfsJson.getValue());
                bigDecimal=bigDecimal.add(new BigDecimal(hgfsJson.getValue()));
            }
        }
        bigDecimal=bigDecimal.multiply(new BigDecimal("0.15"));
        if(bigDecimal.compareTo(BigDecimal.valueOf(300))>-1){//流水大于了300
            bigDecimal=BigDecimal.valueOf(300);
        }
        BigDecimal yuguMoney=bigDecimal.subtract(xyMoney);
        if(yuguMoney.compareTo(BigDecimal.valueOf(300))==1){
            taskList.setYugumoney(BigDecimal.valueOf(300));
        }else{
            if(yuguMoney.compareTo(BigDecimal.ZERO)<1){
                taskList.setYugumoney(BigDecimal.ZERO);
            }else{
                if(yuguMoney.compareTo(BigDecimal.valueOf(30))<1){
                    taskList.setYugumoney(BigDecimal.valueOf(30));
                }else{
                    taskList.setYugumoney(yuguMoney);
                }

            }
        }
        if(taskList.getManagerId()!=null&&!taskList.getManagerId().equals("")){
            SysManager sysManager=this.iSysManagerService.getById(taskList.getManagerId());
            if(sysManager!=null){
                if(sysManager.getMType()==6){
                    taskList.setFwzxzrManagerId(sysManager.getParentManagerId());
                    taskList.setFwzxygManagerId(taskList.getManagerId());
                }else{
                    taskList.setFwzxzrManagerId(sysManager.getManagerId());
                }
                taskList.setMname("");
                taskList.setManagerId("");
                taskList.setStatus(0);
                taskList.setSoureData(sysManager.getMName());
                taskList.setSoureManagerId(taskList.getManagerId());
            }else{
                taskList.setMname("");
                taskList.setManagerId("");
                taskList.setStatus(0);
                taskList.setSoureData("系统");
            }
        }else{
            taskList.setMname("");
            taskList.setManagerId("");
            taskList.setStatus(0);
            taskList.setSoureData("系统");
        }
        taskList.setSetup(1);
        this.save(taskList);
        return ResultView.ok(taskList.getYugumoney());
    }

    @Override
    public ResultView getPageList(QPage qPage, TaskListSearch obj) {
        IPage iPage = new Page(qPage.getOffset(), qPage.getLimit());
        List<TaskListView> mapList = this.baseMapper.getPageList(iPage,obj);
        return ResultView.ok(iPage.setRecords(mapList));
    }

    @Override
    public ResultView fenpei(String taskId, String mname,String mname2) {
        String managerId= StpUtil.getLoginIdAsString();
        SysManager dto=this.iSysManagerService.getById(managerId);
        TaskList taskList=this.getById(taskId);
        if(taskList.getStatus()==4){
            return ResultView.error("数据已完结，无法重复操作");
        }
        if(dto.getMType()==1){//超级管理员处理方式
            if(taskList.getBankName()!=null&&!taskList.getBankName().equals("")){
                return ResultView.error("数据已交付银行，不可再次分配");
            }
            SysManager sysManager=this.iSysManagerService.getById(mname);
            taskList.setStatus(2);
            taskList.setJhtdzrManagerId(sysManager.getParentManagerId());
            taskList.setJhtdygManagerId(mname);
            taskList.setManagerId(mname);
            taskList.setMname(sysManager.getMName());
            if(StringUtils.isNotBlank(mname2)){//如果分配银行不为空-发送短信通知银行
                SysManager bankManager=this.iSysManagerService.getById(mname2);
                taskList.setBankName(bankManager.getMName());
                taskList.setStatus(3);
                taskList.setYhtdzrManagerId(bankManager.getParentManagerId());
                taskList.setYhtdygManagerId(mname2);
                //异步发送短信接口
                this.iSmsService.SendSms(bankManager.getMPhone(),bankManager.getMName());
                //taskList.setManagerId(mname);
                //taskList.setMname(bankManager.getMName());
            }
            taskList.setOperManagerId(dto.getManagerId());
            taskList.setOperTime(new Date());
        }else{//普通账号处理方式-0，未分配，1.服务中心，2.交互团队，3.银行团队，4.已完结
            if(taskList.getStatus()==0){//服务中心-只能处理未分配到交互团队
                if(dto.getMType()!=1&&dto.getMType()!=2){
                    return ResultView.error("此数据你已分配到下个流程，无法重新分配");
                }
                SysManager sysManager=this.iSysManagerService.getById(mname);
                taskList.setStatus(2);
                taskList.setJhtdzrManagerId(sysManager.getParentManagerId());
                taskList.setJhtdygManagerId(mname);
                taskList.setManagerId(mname);
                taskList.setMname(sysManager.getMName());
                taskList.setOperManagerId(dto.getManagerId());
                taskList.setOperTime(new Date());
            }else if(taskList.getStatus()==2){////交互团队主任-只能分配给交互团队员工--//交互团队员工-分配给银行
                if(taskList.getBankName()!=null&&!taskList.getBankName().equals("")){
                    return ResultView.error("数据已交付银行，不可再次分配");
                }
                if(!StringUtils.isNotBlank(taskList.getBankName())){
                    if(dto.getMType()==2){//并且是主任分配的请客
                        SysManager sysManager=this.iSysManagerService.getById(mname);
                        taskList.setStatus(2);
                        taskList.setJhtdzrManagerId(sysManager.getParentManagerId());
                        taskList.setJhtdygManagerId(mname);
                        taskList.setManagerId(mname);
                        taskList.setMname(sysManager.getMName());
                    }else{
                        if(dto.getMType()!=1&&dto.getMType()!=4&&dto.getMType()!=6){
                            return ResultView.error("此数据你已分配到下个流程，无法重新分配");
                        }
                        SysManager sysManager=this.iSysManagerService.getById(mname);
                        taskList.setStatus(3);
                        taskList.setYhtdzrManagerId(sysManager.getParentManagerId());
                        taskList.setYhtdygManagerId(mname);
                        //taskList.setManagerId(mname);
                        //taskList.setMname(sysManager.getMName());
                        taskList.setBankName(sysManager.getMName());
                        //异步发送短信接口
                        this.iSmsService.SendSms(sysManager.getMPhone(),sysManager.getMName());
                    }
                }else{
                    if(dto.getMType()!=1&&dto.getMType()!=4&&dto.getMType()!=6){
                        return ResultView.error("此数据你已分配到下个流程，无法重新分配");
                    }
                    SysManager sysManager=this.iSysManagerService.getById(mname);
                    taskList.setStatus(3);
                    taskList.setYhtdzrManagerId(sysManager.getParentManagerId());
                    taskList.setYhtdygManagerId(mname);
                    //taskList.setManagerId(mname);
                    //taskList.setMname(sysManager.getMName());
                    taskList.setBankName(sysManager.getMName());
                    //异步发送短信接口
                    this.iSmsService.SendSms(sysManager.getMPhone(),sysManager.getMName());
                }
            }else if(taskList.getStatus()==3){//银行那边了
                if(taskList.getBankName()!=null&&!taskList.getBankName().equals("")){
                    return ResultView.error("数据已交付银行，不可再次分配");
                }
            }
        }

        taskList.setUpdatetime(new Date());
        this.updateById(taskList);
        return ResultView.ok();
    }

    @Override
    public List<TasklistModel> downLoadList(TaskListSearch obj) {
        return baseMapper.downLoadList(obj);

    }

    @Override
    public ResultView getListId(String taskId) {
        TaskListView taskListView=this.baseMapper.getListId(taskId);
        return ResultView.ok(taskListView);
    }

    @Override
    public ResultView del(String taskId) {
        TaskList taskList=this.getById(taskId);
        if(taskList!=null){
            this.removeById(taskId);
        }
        return ResultView.ok();
    }

    @Override
    public ResultView tuijian(String taskId, String applyConten) {
        TaskList taskList=this.getById(taskId);
        if(taskList.getStatus()==4){
            return ResultView.error("数据已完结，无法进行退件操作");
        }
        taskList.setApplyStatus("2");
        taskList.setApplyContent(applyConten);
        taskList.setStatus(4);
        this.updateById(taskList);
        //发送短信提醒哥哥
        if(!taskList.getSoureData().equals("系统")){
            if(StringUtils.isNotBlank(taskList.getSoureManagerId())){
                SysManager sysManager=this.iSysManagerService.getById(taskList.getManagerId());
                if(sysManager!=null&&sysManager.getMAccount().length()==11){
                    iSmsService.SendSmsQy(sysManager.getMAccount(),sysManager.getMName(),taskList.getGsmc());
                }else{
                    log.error("手机号不符合规则，不推送{"+sysManager.getMAccount()+"}");
                }
            }else{
                String soureDate=taskList.getSoureData();
                QueryWrapper<SysManager> queryWrapper=new QueryWrapper<>();
                queryWrapper.eq("m_name",soureDate);
                List<SysManager> sysManagers=this.iSysManagerService.list(queryWrapper);
                if(sysManagers!=null&&sysManagers.size()>=1){
                    SysManager sysManager=sysManagers.get(0);
                    if(sysManager!=null&&sysManager.getMAccount().length()==11){
                        iSmsService.SendSmsQy(sysManager.getMAccount(),sysManager.getMName(),taskList.getGsmc());
                    }else{
                        log.error("手机号不符合规则，不推送{"+sysManager.getMAccount()+"}");
                    }
                }else{
                    log.error("账号没找到，不推送{"+taskList.getTaskId()+"}");
                }
            }
        }else{
            log.error("二维码来自系统数据，不推送{"+taskList.getTaskId()+"}");
        }

        return ResultView.ok();
    }


    public static void main(String[] args) {
        Integer Mtype=2;
        if(!Mtype.equals(1)&&!Mtype.equals(2)){
            System.out.println("1");
        }
        System.out.println(2);
    }


}
